package cn.takujo.takujoframework.common.util.code_generator;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import cn.takujo.takujoframework.common.util.code_generator.JavaBean.PrivateField;

/**
 * java代码生成器<br>
 * 代码存放位置（src/main/resources/code/java）
 * 
 * @author wzx
 *
 */
public class JavaCodeGenerator {

	private static DatabaseHandler databaseHandler;
	private static boolean isGenMapper = false;
	private static boolean isLombok = false;
	private static String mainPackName;
	private static String[] tableLikes;

	/**
	 * 一键生成（包含init和exe方法）
	 * 
	 * @param url
	 *            数据库链接
	 * @param user
	 *            用户名
	 * @param password
	 *            密码
	 * @param database
	 *            数据库名
	 * @param mainPackName
	 *            主包名
	 * @param isGenMapper
	 *            是否生成MybatisMapper
	 * @param isLombok
	 *            是否以lombok规则生成javaBean
	 * @param tableLikes
	 *            根据指定的表去生成（表的模糊名），不需要则传入null，则全部生成
	 */
	public static void gen(String url, String user, String password, String database, String mainPackName,
			boolean isGenMapper, boolean isLombok, String[] tableLikes) {
		init(url, user, password, database, mainPackName, isGenMapper, isLombok, tableLikes);
		exe();
	}

	/**
	 * 初始化方法
	 * 
	 * @param url
	 *            数据库链接
	 * @param user
	 *            用户名
	 * @param password
	 *            密码
	 * @param database
	 *            数据库名
	 * @param mainPackName
	 *            主包名
	 * @param isGenMapper
	 *            是否生成MybatisMapper
	 * @param isLombok
	 *            是否以lombok规则生成javaBean
	 * @param tableLikes
	 *            根据指定的表去生成（表的模糊名），不需要则传入null，则全部生成
	 */
	public static void init(String url, String user, String password, String database, String mainPackName,
			boolean isGenMapper, boolean isLombok, String[] tableLikes) {
		DbUtil.setUrl(url);
		DbUtil.setUser(user);
		DbUtil.setPassword(password);
		databaseHandler = new DatabaseHandler(database);
		JavaCodeGenerator.isGenMapper = isGenMapper;
		JavaCodeGenerator.isLombok = isLombok;
		JavaCodeGenerator.mainPackName = mainPackName;
		JavaCodeGenerator.tableLikes = tableLikes;
	}

	/**
	 * 执行方法
	 */
	public static void exe() {
		List<String> findTableList = databaseHandler.findTableList();
		for (String tableName : findTableList) {
			if (isGen(tableName)) {
				List<Map<String, Object>> findFieldList = databaseHandler.findFieldList(tableName);
				int size = findFieldList.size();
				PrivateField<?>[] fields = new PrivateField[size];
				for (int i = 0; i < size; i++) {
					Map<String, Object> map = findFieldList.get(i);
					String ct = (String) map.get("column_type");
					String col = (String) map.get("column_name");
					PrivateField<?> pf = new PrivateField<>(DatabaseHandler.javaType(ct), null, col);
					fields[i] = pf;
				}
				JavaBean bean = new JavaBean(null, tableName, mainPackName + ".bean", fields, isLombok);
				bean(bean);
				if (isGenMapper) {
					MybatisMapper mmr = new MybatisMapper(null, mainPackName + ".mapper", bean);
					mapper(mmr);
				}
			}
		}
	}

	public static void bean(JavaBean bean) {
		try {
			JavaTemplate.process(bean.toMap(), "bean/Bean", "bean/" + bean.getBeanName() + "Bean");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void mapper(MybatisMapper mapper) {
		try {
			JavaTemplate.process(mapper.toMap(), "mapper/Mapper", "mapper/" + mapper.getInterfaceName() + "Mapper");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static boolean isGen(String tableName) {
		if (tableLikes != null) {
			for (String str : tableLikes) {
				if (tableName.contains(str)) {
					return true;
				}
			}
			return false;
		} else {
			return true;
		}
	}

}
